//
//  TextFieldView.m
//
//

#import "TextFieldView.h"

@implementation TextFieldView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor=[UIColor grayColor];
        CGRect VFrame = self.frame;
        CGFloat Icon_W = 25;
        
//        self.layer.cornerRadius= VFrame.size.height/2;
        self.layer.borderWidth=_ViewBorderWidth;
        self.layer.borderColor=_ViewBorderColor.CGColor;
        
        self.leftIcon = [UIButton buttonWithType:UIButtonTypeCustom];
        self.leftIcon.frame=CGRectMake(10, 0, Icon_W, VFrame.size.height);
        [self addSubview:self.leftIcon];
        
        CGFloat leftFloat_w= self.leftIcon.frame.size.width+self.leftIcon.frame.origin.x;
        //textField
        self.TextF = [[UITextField alloc]initWithFrame:CGRectMake(leftFloat_w, 0, VFrame.size.width-80, VFrame.size.height)];
        self.TextF.delegate =self;
        self.TextF.keyboardType= UIKeyboardTypeNumberPad;
        //kvc自定义_clearButton
        UIButton *clearButton = [self.TextF valueForKey:@"_clearButton"];
        [clearButton setImage:[UIImage imageNamed:@"filter_check"] forState:UIControlStateNormal];
        self.TextF.clearButtonMode=UITextFieldViewModeAlways;
        [self addSubview:self.TextF];
        
        
        //添加通知监听文本状态
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:self.TextF];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidEndEditing:) name:UITextFieldTextDidEndEditingNotification object:self.TextF];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextFieldTextDidChangeNotification object:self.TextF];
        
        
    }
    return self;
}
#pragma mark - 配置
//限定键盘
-(void)setKeyBoardType:(UIKeyboardType *)KeyBoardType
{
    
}
//
-(void)setPlaceholderString:(NSString *)placeholderString
{
    self.TextF.placeholder=placeholderString;
}
-(void)textBeginEditing:(NSNotification*)note
{
    [self changBorderwithNote:note];
}
-(void)textDidEndEditing:(NSNotification*)note
{
    [self changBorderwithNote:note];
}

-(void)textDidChange:(NSNotification*)note
{
    [self changBorderwithNote:note];
}
-(void)changBorderwithNote:(NSNotification*)editing
{
    if (![editing.object isEqual:self.TextF])return;
    if ([editing.name isEqualToString:UITextFieldTextDidBeginEditingNotification])
    {
//        self.layer.borderColor=[UIColor blueColor].CGColor;
        
    }else if ([editing.name isEqualToString:UITextFieldTextDidEndEditingNotification])
    {
//        self.layer.borderColor=[UIColor blueColor].CGColor;
        
    }else if ([editing.name isEqualToString:UITextFieldTextDidChangeNotification]){
        
        if (self.maxTextLength!=0)
        {
            if (self.TextF.text.length >self.maxTextLength) {
                [self judemaxText];
            }
        }
    }
    
}
#pragma mark - 限制最大输入字数
-(void)judemaxText
{
    if (_maxTextLength>0)
    {
        // 键盘输入模式
        NSString *lang=[[UIApplication sharedApplication]textInputMode].primaryLanguage;
        if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
            UITextRange *selectedRange = [self.TextF markedTextRange];
            //获取高亮部分
            UITextPosition *position = [self.TextF positionFromPosition:selectedRange.start offset:0];
            // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
            if (!position) {
                if (self.TextF.text.length >= _maxTextLength) {
                    if (self.TextF.text.length>_maxTextLength)
                    {
//                        [self addShakeAnimation];
                    }
                    NSString *newText=[self.TextF.text substringToIndex:_maxTextLength];
                    if (![self.TextF.text isEqualToString:newText])
                    {
                        self.TextF.text =newText;
                    }
                    
                }
            }
            // 有高亮选择的字符串，则暂不对文字进行统计和限制
            else{
                
            }
        }
        // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
        else{
            if (self.TextF.text.length >= _maxTextLength) {
                if (self.TextF.text.length>_maxTextLength)
                {
//                    [self addShakeAnimation];
                }
                NSString *newText=[self.TextF.text substringToIndex:_maxTextLength];
                if (![self.TextF.text isEqualToString:newText])
                {
                    self.TextF.text =newText;
                }
            }
        }
        
    }
    
}
#pragma mark - 添加抖动动画
-(void)addShakeAnimation
{
    CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
    CGFloat currentTx = self.transform.tx;
    
    //    animation.delegate = self;
    animation.duration = 0.5;
    animation.values = @[ @(currentTx), @(currentTx + 10), @(currentTx-8), @(currentTx + 8), @(currentTx -5), @(currentTx + 5), @(currentTx) ];
    animation.keyTimes = @[ @(0), @(0.225), @(0.425), @(0.6), @(0.75), @(0.875), @(1) ];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.layer addAnimation:animation forKey:@"kAFViewShakerAnimationKey"];
    [self.layer setBorderColor:RGB(255, 0, 0).CGColor];
}


@end
